<?php

declare(strict_types=1);

namespace app\model;

use app\model\AgentBalanceLog as Model;

/**
 * 
 *
 * @mixin \think\Model
 * @property int $id 自增id
 * @property int $agent_id 代理商id
 * @property int $type 类型：1提现申请 2提现撤销 3提现驳回 4话费佣金 5话费充值 6后台充值 7充值失败退款 8代理商充值9加油返佣
 * @property int $amount 变动金额
 * @property int $old_balance 旧金额
 * @property int $new_balance 新金额
 * @property string $remark 备注
 * @property int $quote_id 对应记录的id
 * @property string $created_at 创建时间
 * @property string $updated_at 更新时间
 * @property string $trade_no 支付宝交易号
 * @property bool $flow_step 涉及审批的，当前步骤是否完成标识 1 未完成 9 完成
 * @property-read \app\model\Agent $agent
 * @property-read mixed $settle_type_name
 * @property string $stat_at 对接有美-统计的时间，指什么月份产生的订单
 * @property bool $stat_status 对接有美-统计状态0未处理或无需统计1:已申请提现2:完成了提现
 * @property-read mixed $type_name
 * @property-read \app\model\CouponOrder $coupon_order
 * @property-read mixed $order_id
 */
class AgentBalanceLog extends CommonModel
{

    const STAT_STATUS_WAIt = 0;
    const STAT_STATUS_STAT = 1;//已统计

    // constant 里面有相关定义
//    public static $typeArr = [
//        BALANCE_WITHDRAW_APPLY => '提现申请',
//        BALANCE_WITHDRAW_CANCEL => '提现撤销',
//        BALANCE_WITHDRAW_REJECT => '提现驳回',
//        BALANCE_MOBILE_PROFIT => '话费佣金',
//        BALANCE_MOBILE_RECHARGE => '话费扣款',
//        BALANCE_AGENT_RECHARGE => '后台充值',
//        BALANCE_MOBILE_REFUND => '话费充值失败退款',
//        BALANCE_ONLINE_RECHARGE => '在线充值',
//        BALANCE_OIL_COMMISS => '加油佣金',
//        BALANCE_COUPON_COMMISS => '权益佣金',
//        BALANCE_FILM_COMMISS => '影票佣金',
//        BALANCE_COUPON_EXPENSE => '权益扣款',
//        BALANCE_FILM_EXPENSE => '影票扣款',
//        BALANCE_AGENT_REFUND => '预付退款',
//        BALANCE_OILCARD_EXPENSE => '油卡扣款',
//        BALANCE_POWER_EXPENSE => '电费扣款',
//        BALANCE_OILCARD_COMMISS => '油卡佣金',
//        BALANCE_POWER_COMMISS => '电费佣金',
//        BALANCE_TRAVEL_OIL_COMMISS => '出行/加油CPS佣金',
//        BALANCE_AROUND_COMMISS => '周边游CPS佣金',
//        BALANCE_FOOD_COMMISS => '连锁餐饮CPS佣金',
//        BALANCE_MEITUAN_SEACKILL_COMMISS => '美团秒杀CPS佣金',
//        BALANCE_TACKOUT => '美团外卖佣金',
//        BALANCE_TACKOUT_ELE => '饿了么外卖佣金',
//        BALANCE_TACKOUT_MEITUAN_DAODIAN => '美团外卖佣金',
//        BALANCE_MOBILE_CPS_SERV => 'CPS话费佣金',
//        BALANCE_REFUND_APPLY => '退款申请',
//        BALANCE_REFUND_CANCEL => '退款申请撤销',
//        BALANCE_REFUND_REJECT => '退款申请拒绝',
//        BALANCE_OILCARD_REFUND => '加油卡退款',
//        BALANCE_POWER_REFUND => '电费退款',
//        BALANCE_COUPON_REFUND => '权益退款',
//        BALANCE_CHANGE_APPLY => '加款申请',
//    ];

    /**
     * constant 里面有相关定义
     */
    public static function getTypeArr()
    {
        $balance_types = BALANCE_TYPES;
        $arr = array();
        foreach ($balance_types as $key => $balance_type) {
            $arr[$key] = $balance_type['name'];
        }
        return $arr;
    }

    /**
     * 获取资金类型
     */
    public function getTypeNameAttr($value, $model)
    {
        $type = $model['type'];
        $typeArr = self::getTypeArr();
        return $typeArr[$type] ?? '';
    }

    /**
     * 佣金类型的余额变动
     * @return array
     */
    public static function commission()
    {
        return [
            BALANCE_MOBILE_PROFIT,
            BALANCE_OIL_COMMISS,
            BALANCE_COUPON_COMMISS,
            BALANCE_FILM_COMMISS,
            BALANCE_OILCARD_COMMISS,
            BALANCE_POWER_COMMISS,
            BALANCE_TRAVEL_OIL_COMMISS,
            BALANCE_AROUND_COMMISS,
            BALANCE_FOOD_COMMISS,
            BALANCE_MEITUAN_SEACKILL_COMMISS,
            BALANCE_TACKOUT,
            BALANCE_TACKOUT_ELE,
            BALANCE_TACKOUT_MEITUAN_DAODIAN,
            BALANCE_MOBILE_CPS_SERV,

            //扣钱的记录 SupplierFailGoBack 类有使用
            BALANCE_SUPPLIER_FIAL_GOBACK,
        ];
    }

    // 业务扣费
    public function servTypeOut($serType = '')
    {
        $arr = [
            PRODUCT_MOBILE_SERV => BALANCE_MOBILE_RECHARGE,
            PRODUCT_COUPON_SERV => BALANCE_COUPON_EXPENSE,
            PRODUCT_FILM_SERV => BALANCE_FILM_EXPENSE,
            PRODUCT_OILCARD_SERV => BALANCE_OILCARD_EXPENSE,
            PRODUCT_POWER_SERV => BALANCE_POWER_EXPENSE,
        ];

        if ($serType) {
            return $arr[$serType] ?? '';
        }

        return $arr;
    }

    // 业务退款
    public function servTypeRefund($serType = '')
    {
        $arr = [
            PRODUCT_MOBILE_SERV => BALANCE_MOBILE_REFUND,
            PRODUCT_COUPON_SERV => BALANCE_COUPON_REFUND,
            // PRODUCT_FILM_SERV => BALANCE_FILM_EXPENSE,
            PRODUCT_OILCARD_SERV => BALANCE_OILCARD_REFUND,
            PRODUCT_POWER_SERV => BALANCE_POWER_REFUND,
        ];

        if ($serType) {
            return $arr[$serType] ?? '';
        }

        return $arr;
    }

    /*
    // 资金流向类型
    public $flowType = [
        1 => '业务入账',
        2 => '业务支出',
        3 => '系统入账',
        4 => '系统支出',
    ];
    // 系统资金流向子类型
    public $sysServType = [
        1 => '提现',
        2 => '在线充值',
        3 => '后台充值',
    ]; */

    public function agent()
    {
        return $this->belongsTo(Agent::class, 'agent_id');
    }

    public function couponOrder()
    {
        return $this->belongsTo(CouponOrder::class, 'quote_id');
    }

    public function searchOrderIdAttr($query, $value, $params)
    {
        $order_id = $params['order_id']??'';
        if ($order_id){
            return $query->where('trade_no',$value);
        }

    }

    /**
     * 订单号
     */
    public function getOrderIdAttr($value, $model)
    {
        return $this->trade_no;
    }

    /**
     * 代理商的结算类型
     * @param $value
     * @param $model
     */
    public function getSettleTypeNameAttr($value, $model)
    {
        $settle_type = $model['settle_type'] ?? '';
        return Agent::$settleType[$settle_type] ?? '未知';

    }
}
